package com.umeox.mediaplayer

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yc.music.model.AudioBean

class MusicListAdapter :
    RecyclerView.Adapter<MusicListAdapter.ItemViewHolder>() {
    var itemClickListener: ItemClickListener? = null
    private var movieBeans = mutableListOf<AudioBean>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_music, parent, false)
        return ItemViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val movieBean = movieBeans[position]
        holder.bind(movieBean)
        holder.itemView.setOnClickListener { itemClickListener?.onItemClicked(movieBean) }
        holder.itemView.setOnLongClickListener {
            itemClickListener?.onItemLongClick(movieBean)
            return@setOnLongClickListener false
        }
    }

    fun setDataList(list: List<AudioBean>) {
        movieBeans.clear()
        movieBeans.addAll(list)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return movieBeans.size
    }

    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val mNameTv: TextView = itemView.findViewById(R.id.tv_name)
        private val mDurationTv: TextView = itemView.findViewById(R.id.tv_singer)

        fun bind(movieBean: AudioBean) {
            mNameTv.text = movieBean.title
            mDurationTv.text = movieBean.artist

        }
    }

    interface ItemClickListener {
        fun onItemClicked(position: AudioBean)

        fun onItemLongClick(position: AudioBean)
    }
}